<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="名称" prop="nickName">
                <el-input v-model="queryParams.nickName" placeholder="请输入名称" clearable
                maxlength="10" show-word-limit    @keyup.enter.native="handleQuery" />
            </el-form-item>
              <el-form-item label="联系电话" prop="phonenumber">
                <el-input v-model="queryParams.phonenumber" placeholder="请输入联系电话" clearable maxlength="11" show-word-limit
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
             <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['business:member:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="configurationList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" width="55" type="index" />
            <el-table-column label="头像" align="center" prop="avatar" min-width="100" :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <img :src="scope.row.avatar" alt="" style="width: 40px;height: 40px;">
                </template>
            </el-table-column>
            <el-table-column label="名称" align="center" prop="nickName" min-width="100" :show-overflow-tooltip="true" />
            <el-table-column label="联系电话" align="center" prop="phonenumber" min-width="100"
                :show-overflow-tooltip="true" />
            <el-table-column label="性别" align="center" prop="sex" min-width="100" :show-overflow-tooltip="true">
                <template v-slot="scope">
                    <div v-if="scope.row.sex == 0">男</div>
                    <div v-if="scope.row.sex == 1">女</div>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="80">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
                        v-hasPermi="['member:member:edit']">修改</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改配置维护对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="头像" prop="avatar">
                    <image-upload v-model="form.avatar" />
                </el-form-item>
                <el-form-item label="名称" prop="nickName">
                    <el-input v-model="form.nickName" placeholder="请输入名称" />
                </el-form-item>
                <el-form-item label="联系电话" prop="phonenumber">
                    <el-input v-model="form.phonenumber" placeholder="请输入联系电话" />
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择性别">
                        <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                            :value="dict.value"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { memberList, getMember,updateMember } from '@/api/member/index.js'
export default {
    name: "ConfigurationMember",
    dicts: ['sys_user_sex'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组q
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 配置维护表格数据
            configurationList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                nickName:undefined,
                pageNum: 1,
                pageSize: 10,
                userType: 1,
                phonenumber:undefined
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                phonenumber: [
                    {
                        pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
                        message: "请输入正确的手机号码",
                        trigger: "blur"
                    }
                ],
                avatar: [
                    { required: true, message: "头像不能为空", trigger: "blur" }
                ],
                sex: [
                    { required: true, message: "性别不能为空", trigger: "blur" }
                ],
                nickName: [
                    { required: true, message: "名称不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
          /** 导出按钮操作 */
        handleExport() {
            this.download('/business/member/export', {
                ...this.queryParams,
                ids:this.ids
            }, `专家排班_${new Date().getTime()}.xlsx`)
        },
        /** 查询配置维护列表 */
        getList() {
            this.loading = true;
            memberList(this.queryParams).then(response => {
                this.configurationList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                phonenumber: null,
                avatar: null,
                sex: null,
                nickName: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.userId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.userId
            getMember(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改会员信息";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    updateMember(this.form).then(response => {
                        this.$modal.msgSuccess("修改成功");
                        this.open = false;
                        this.getList();
                    });
                }
            });
        }
    }
};
</script>
